package com.ghsc.ghxc.jsb.ui.message.fragment

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import androidx.fragment.app.FragmentManager
import com.cn.appcore.base.BaseVMFragment
import com.cn.appcore.ext.loge
import com.cn.appcore.utils.GsonUtil
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.databinding.FragmentGroupListBinding
import com.ghsc.ghxc.jsb.ui.message.MessageViewModel
import com.ghsc.ghxc.jsb.ui.message.activity.ChatSetSingleActivity
import com.ghsc.ghxc.jsb.ui.message.activity.ChatTeamSetActivity
import com.ghsc.ghxc.jsb.ui.message.bean.ChatGroupExtBean
import com.netease.nimlib.sdk.NIMClient
import com.netease.nimlib.sdk.msg.MsgService
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum
import com.netease.nimlib.sdk.msg.model.StickTopSessionInfo
import com.netease.nimlib.sdk.team.TeamService
import com.netease.yunxin.kit.chatkit.repo.ConversationRepo
import com.netease.yunxin.kit.conversationkit.ui.ConversationKitClient
import com.netease.yunxin.kit.conversationkit.ui.ConversationUIConfig
import com.netease.yunxin.kit.conversationkit.ui.ItemClickListener
import com.netease.yunxin.kit.conversationkit.ui.model.ConversationBean
import com.netease.yunxin.kit.conversationkit.ui.normal.page.ConversationFragment
import com.netease.yunxin.kit.corekit.im.provider.FetchCallback

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.message.fragment
 * 类名：YunXinCharListFragment
 * 创建时间：2024/5/7 14:53
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class YunXinCharListFragment  : BaseVMFragment<MessageViewModel, FragmentGroupListBinding>(R.layout.fragment_group_list) {
    companion object {
        fun getInstance(): YunXinCharListFragment? {
            val fragment = YunXinCharListFragment()
            return fragment
        }
    }
    override fun attachVMClass(): Class<MessageViewModel>? {
        return MessageViewModel::class.java
    }

    override fun lazyLoad() {

    }

    override fun initView(savedInstanceState: Bundle?) {
        FragmentDataList()
    }

    /**
     * 加载会话列表
     */
    private fun FragmentDataList() {
        val fragmentManager: FragmentManager = childFragmentManager
//        var manager = requireActivity().supportFragmentManager
        //创建ConversationFragment
        val fragment = ConversationFragment()
        fragmentManager
            .beginTransaction()
            .add(R.id.conversation_container, fragment)
            .commit()
        //1、实现点击事件
        val config = ConversationUIConfig()
        //2、设置TitleBar不可见
        config.showTitleBar = false
        //3、将自定义事件设置到ConversationKitClient即可
        ConversationKitClient.setConversationUIConfig(config)
        config.itemClickListener=object : ItemClickListener {
            override fun onAvatarClick(
                context: Context?,
                data: ConversationBean?,
                position: Int
            ): Boolean {
                loge("会话列表-头像点击事件=="+data!!.viewType)
                data.let {
                    when(it.viewType){
                        1 ->{
                            if (it.infoData.userInfo!!.account != CustKey.CHAT_SYSY_ID){
                                // 单聊
                                var  bundle = Bundle()
                                bundle.putString("userIdTa",it.infoData.userInfo!!.account)
                                startActivity(ChatSetSingleActivity().javaClass,bundle)
                            }
                        }
                        2 ->{
                            // 群聊
                            val teamInfoBean = NIMClient.getService(TeamService::class.java).queryTeamBlock(it.infoData.teamInfo!!.id)
                            if (!TextUtils.isEmpty(teamInfoBean!!.extServer)){
                                val chatGroupExtBean = GsonUtil.GsonToJsonClass(teamInfoBean.extServer,
                                    ChatGroupExtBean::class.java)
                                loge("会话列表-头像点击事件=="+it.infoData.teamInfo!!.id +"=="+chatGroupExtBean.gid+"xxx="+chatGroupExtBean.tid)
                                var  bundle = Bundle()
                                bundle.putString("targetId",chatGroupExtBean.gid)
                                bundle.putString("tId",chatGroupExtBean.tid)
                                bundle.putInt("type",3)
                                startActivity(ChatTeamSetActivity().javaClass,bundle)
                            }
                        }
                    }
                }
                return true
            }

        }

        ConversationRepo.addStickTop(CustKey.CHAT_SYSY_ID, SessionTypeEnum.P2P,"",object :
            FetchCallback<StickTopSessionInfo> {
            override fun onException(exception: Throwable?) {
            }

            override fun onFailed(code: Int) {
            }

            override fun onSuccess(param: StickTopSessionInfo?) {

            }

        })
    }
}